package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;

import cn.itcast.pojo.Address;
import cn.itcast.service.AddressService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分院管理
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Reference
    private AddressService addressService;

    /**
     * 查询所有 地址
     *
     * @return
     */

    @RequestMapping("/findAll")
    public Result findAll() {
        try {
          //数据不只有一条故使用List集合
           List<Address> addressList = addressService.findAll();
          return new Result(true, MessageConstant.QUERY_ADDRESS_SUCCESS, addressList);
       } catch (Exception e) {
            e.printStackTrace();
           return new Result(false, MessageConstant.QUERY_ADDRESS_FAIL);
       }
    }

//    //分页查询
//    @RequestMapping("/findPage")
//    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
//        System.out.println(queryPageBean.getPageSize());
//        System.out.println(queryPageBean.getCurrentPage());
//        System.out.println(queryPageBean.getQueryString());
//        System.out.println(addressService.pageQuery(queryPageBean).getRows());
//        System.out.println(addressService.pageQuery(queryPageBean).getTotal());
//        return addressService.pageQuery(queryPageBean);
//    }

    /**
     * 添加地址
     */

    @PostMapping(path = "/submit")
    public Result add(@RequestBody Address address) {
        try {
            //submit是点击事件确定按钮通过确定添加数据
            addressService.add(address);
            return new Result(true, MessageConstant.ADD_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ADDRESS_FAIL);
        }
    }

    /**
     * 删除地址
     *
     * @param id
     * @return
    */
    @DeleteMapping(path = "/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        try {
            //删除分公司
            addressService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_ADDRESS_SUCCESS);
        } catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ADDRESS_FAIL);
        }
    }

    /**
     * 更新因为是可以渠道参数 所以使用 @RequestBody
     * @param address
     * @return
     */

    @PutMapping("/update")
    public Result update(@RequestBody Address address) {
        try {
            addressService.update(address);
            return new Result(true, MessageConstant.EDIT_ADDRESS_SUCCESS);
        } catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ADDRESS_FAIL);
        }
    }

    /**
     * 根据ID 判断 当前操作的是那条数据
     * @param id
     * @return
     */
    @GetMapping(path = "/{id}")
    public Result findById(@PathVariable Integer id) {
        try {
            // findById()查询返回数据
          Address address =   addressService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,address);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     *  给 Enable 绑定的事件 用change方法 改变MySQL中 Enable
     * @param id
     * @return
     */

    @PutMapping("{id}")
    public Result change(@PathVariable Integer id ) {
        try {
            /**
             * 通过ID  调用 sql语句的 update方法
             *               update t_address set enable = !enable where  id = #{id}  取反
             */
            addressService.change(id);
            return new Result(true, MessageConstant.CHANGE_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(true, MessageConstant.CHANGE_ADDRESS_FAIL);
    }

}
